package com.example.ice.fittime;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.ice.fittime.adapter.SocialRecyclerAdapter;
import com.example.ice.fittime.entry.UserActivity;
import com.example.ice.fittime.global.ClassType;
import com.example.ice.fittime.util.NetUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Social_Act_Activity extends AppCompatActivity implements View.OnClickListener , Callback<List>, SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView act_recyclerview;
    private SocialRecyclerAdapter adapter;
    private ArrayList list = new ArrayList<>();
    private Toolbar social_act_toolbar;
    private ImageView messagehome;
    private ImageView searchhome;
    private boolean isloading;
    private int pager ;
    private TextView header_name;
    private TextView header_update;
    private ImageView header_pic;
    private TextView header_name1;
    private TextView header_update1;
    private ImageView header_pic1;
    private RelativeLayout coll1;
    private RelativeLayout coll2;
    private SwipeRefreshLayout act_swipe;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_social__act_);

        social_act_toolbar = (Toolbar) findViewById(R.id.social_act_toolbar);
        act_recyclerview = (RecyclerView) findViewById(R.id.social_actrecycleview);
        act_swipe = (SwipeRefreshLayout) findViewById(R.id.social_act_swipe);
        act_swipe.setOnRefreshListener(this);
        setSupportActionBar(social_act_toolbar);
        Intent intent = getIntent();
         type = intent.getStringExtra("type");
        long time = new Date().getTime();
        if (type.equals("hot")) {
            setTitle("热门内容");
            String json = intent.getStringExtra("json");
            list = new Gson().fromJson(json,new TypeToken<List<UserActivity>>(){}.getType());

           NetUtil.getData(ClassType.UserActivity,this,"hot",Long.toBinaryString(time),"30","0");
        }
        if (type.equals("all")) {
            setTitle("最新内容");
           loadUADataMore(pager,type,30);
        }
        adapter = new SocialRecyclerAdapter(list, this, this);
        act_recyclerview.setAdapter(adapter);
        social_act_toolbar.setNavigationIcon(R.mipmap.ic_back_black);

        final AppBarLayout social_appbar = (AppBarLayout) findViewById(R.id.social_act_appbar);
         coll1 = (RelativeLayout) findViewById(R.id.social_act_coll1);
         coll2 = (RelativeLayout) findViewById(R.id.social_act_coll2);


        header_name =(TextView) coll1.findViewById(R.id.social_recycler_item2_header_name);

        header_update = (TextView) coll1.findViewById(R.id.social_recycler_item2_header_update);
        header_pic = (ImageView) coll1.findViewById(R.id.social_recycler_item2_header_pic);

        header_name1 =(TextView) coll2.findViewById(R.id.social_recycler_item2_header_name);
        header_update1 = (TextView) coll2.findViewById(R.id.social_recycler_item2_header_update);
        header_pic1 = (ImageView) coll2.findViewById(R.id.social_recycler_item2_header_pic);
        scrollListener(coll1,coll2);
        itemDecoration();
    }

    @Override
    public void onClick(View v) {

    }
    @Override
    public void onResponse(Call<List> call, Response<List> response) {
        List body = response.body();
        adapter.addAll(body);

    }

    @Override
    public void onFailure(Call<List> call, Throwable t) {

    }

    private void scrollListener(final RelativeLayout coll1, final RelativeLayout coll2) {
        act_recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

                int last = layoutManager.findLastVisibleItemPosition();
                int first = layoutManager.findFirstVisibleItemPosition();

                if (first>=adapter.getItemCount()-5) {
                    loadUADataMore(++pager ,type,30);
                }
                View firstView = layoutManager.findViewByPosition(first);
                View lastView = layoutManager.findViewByPosition(last);
                int Ltop= 0;
                int fTop = 0;
                if(lastView !=null) {
                    Ltop = lastView.getTop();
                }
                if (firstView !=null) {
                    fTop = firstView.getTop();
                }



                UserActivity userActivity = adapter.getObjectItem(first);
                if (userActivity != null) {
                    header_name.setText(userActivity.getUserDetail().getName());
                    header_update.setText("22小时前");
                    adapter.loadCircularImageView(header_pic, userActivity.getUserDetail().getAvatar_url());
                    }
                    if (last >=1&& Ltop <= coll1.getMeasuredHeight()) {
                        coll2.setVisibility(View.VISIBLE);

                        if (Ltop >= 0 && Ltop <= coll1.getMeasuredHeight()) {
                            UserActivity userActivity1 = adapter.getObjectItem(last);
                            if (userActivity != null) {
                                header_name1.setText(userActivity1.getUserDetail().getName());
                                header_update1.setText("22小时前");
                                adapter.loadCircularImageView(header_pic1, userActivity1.getUserDetail().getAvatar_url());
                            }
                            int detalY = 0;
                            detalY = Ltop - coll1.getMeasuredHeight();
                            ViewCompat.setTranslationY(coll1, detalY);
                            ViewCompat.setTranslationY(coll2, detalY);
                        }
                    }
                if( dy<0 &&  (Ltop>=coll1.getMeasuredHeight()-2 && Ltop<=coll1.getMeasuredHeight()+2)){
                    coll2.setVisibility(View.INVISIBLE);
                }

            }
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

        });
    }

    private void itemDecoration() {
        act_recyclerview.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
                c.drawColor(Color.GRAY);
            }
            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,2);
            }
        });
    }

    public void loadUADataMore(int pager ,String type ,int limit ){
        long time = new Date().getTime();
        NetUtil.getData(ClassType.UserActivity,this,type,Long.toBinaryString(time),limit+"",""+pager);
        isloading =!isloading;
    }


    @Override
    public void onRefresh() {
        adapter.clear();
        loadUADataMore(0,type,30);
    }
}
